package com.study.forumsystemproject.controller;

import com.study.forumsystemproject.common.Result;
import com.study.forumsystemproject.common.ResultCode;
import com.study.forumsystemproject.model.Article;
import com.study.forumsystemproject.model.ArticleReply;
import com.study.forumsystemproject.model.User;
import com.study.forumsystemproject.service.IArticleReplyService;
import com.study.forumsystemproject.service.IArticleService;
import com.study.forumsystemproject.service.IUserService;
import com.study.forumsystemproject.utils.JwtUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * program: forum-system-project
 * <p>
 * description: 用户回复的控制层
 * <p>
 * author: xiaoxie
 * <p>
 * create: 2024-08-19 10:18
 **/
@RequestMapping("/reply")
@RestController
@Slf4j
@Tag(name = "回复测试API")
public class ArticleReplyController {

    @Autowired
    private IArticleReplyService articleReplyService;
    @Autowired
    private IArticleService articleService;
    @Autowired
    private IUserService userService;
    @Operation(summary = "添加文章的API")
    @PostMapping("/add")
    public Result<Boolean> addArticleReply(HttpServletRequest request,
                                           @RequestBody @Parameter(description = "帖子回复对象") @Valid ArticleReply articleReply) {
        Long authorId = ((Number) JwtUtil.getValue(request,"UserId")).longValue();
        articleReply.setPostUserId(authorId);
        //校验用户的状态
        User userInfo = userService.getUserInfo(authorId);
        if (userInfo == null || userInfo.getDeleteFlag() == 1) {
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            return Result.fail(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        if(userInfo.getState() == 1) {
            log.warn(ResultCode.FAILED_USER_BANNED.toString());
            return Result.fail(ResultCode.FAILED_USER_BANNED);
        }
        //校验文章的状态
        Article article = articleService.getArticleById(articleReply.getArticleId());
        if (article == null || article.getDeleteFlag() == 1) {
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            return Result.fail(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        if(article.getState() == 1) {
            log.warn(ResultCode.FAILED_ARTICLE_NOT_STATE.toString());
            return Result.fail(ResultCode.FAILED_ARTICLE_NOT_STATE);
        }
        //执行添加操作
        articleReplyService.addArticleReply(articleReply);
        return Result.success(true);
    }
    @Operation(summary = "获取文章回复列表的API")
    @GetMapping("/getList")
    public Result<List<ArticleReply>> getReplyList(@Parameter(description = "文章Id") @NotNull Long articleId) {
        //校验文章状态
        Article articleById = articleService.getArticleById(articleId);
        if (articleById == null || articleById.getDeleteFlag() == 1) {
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            return Result.fail(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        if(articleById.getState() == 1) {
            log.warn(ResultCode.FAILED_ARTICLE_NOT_STATE.toString());
            return Result.fail(ResultCode.FAILED_ARTICLE_NOT_STATE);
        }
        List<ArticleReply> articleReplyByArticleId = articleReplyService.getArticleReplyByArticleId(articleId);
        return Result.success(articleReplyByArticleId);
    }
}
